#include "window.h"

#include "dummyrenderer.h"
#include "glwidget.h"
#include <QWheelEvent>
#include <QKeyEvent>
#include <QTimer>
#include <QGridLayout>
#include <QDebug>

#include <cmath>

QString modelPaths[2][3] = {
                         {QString("../data/teapot_with_normals.obj"),
                          QString("../data/Terminator head T-800.obj"),
                          QString("../data/62531_MD_Helicopters_MH6_Little_Bird.obj")},
                         { QString("../data/T-Rex_skeleton.obj"),
                          QString("../data/skeleton.obj"),
                          QString("../data/1599_Engine_Chevrolet_Small_Block.obj")}
                       };

Window::Window()
{
#if _OLD_CODE_
    QSurfaceFormat format;
    format.setVersion(3, 1);

    format.setRedBufferSize(8);
    format.setGreenBufferSize(8);
    format.setBlueBufferSize(8);
    format.setAlphaBufferSize(8);
    format.setDepthBufferSize(24);
    format.setSamples(4);

    format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);

    setSurfaceType(QWindow::OpenGLSurface);
    setFormat(format);

    create();
    startTimer(16*1);
// add another timer
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(rotateOneStep()));
    timer->start(150);
#else
    QGridLayout *mainLayout = new QGridLayout;

    for (int i = 0; i < NumRows; ++i) {
        for (int j = 0; j < NumColumns; ++j) {
            QColor clearColor;
            clearColor.setHsv(((i * NumColumns) + j) * 255
                              / (NumRows * NumColumns - 1),
                              255, 63);

            qDebug() << "Creating Widget with file " << modelPaths[i][j];
            glWidgets[i][j] = new GLWidget(modelPaths[i][j], 0);
            glWidgets[i][j]->setClearColor(clearColor);
            QWidget *container = QWidget::createWindowContainer(glWidgets[i][j]);
            glWidgets[i][j]->initialize();

            mainLayout->addWidget(container, i, j);

            connect(glWidgets[i][j], SIGNAL(clicked()),
                    this, SLOT(setCurrentGlWidget()));
        }
    }
    setLayout(mainLayout);

    currentGlWidget = glWidgets[0][0];

 //   QTimer *timer = new QTimer(this);
 //   connect(timer, SIGNAL(timeout()), this, SLOT(rotateOneStep()));
 //   timer->start(20);

 #endif
}

void Window::setRenderer(Renderer* rendrer)
{
#if _OLD_CODE_
    m_rendrer = rendrer;
    m_rendrer->ensureInitialized();
#endif
}

void Window::timerEvent(QTimerEvent*)
{
#if _OLD_CODE_
    m_rendrer->render();
#endif
}

void Window::wheelEvent(QWheelEvent* event)
{
#if _OLD_CODE_
    double deltaX = event->angleDelta().x();
    double deltaY = event->angleDelta().y();

    qDebug() << "Window::wheelEvent(), " << "deltaX: " << deltaX << " deltaY: " << deltaY;

    double factor = 1.1;

    double zoomFactor = deltaY/120 * ((deltaY > 0)? factor: -1/factor);

    m_rendrer->setScale(m_rendrer->scale()*zoomFactor);
#endif
}

void Window::keyPressEvent(QKeyEvent* event)
{
#if _OLD_CODE_
    double xDelta = 0;
    double yDelta = 0;

    double step = 0.1;

    if (event->key() == Qt::Key_Left) {
        xDelta = step;
        m_rotationDir = DirLeft;
    }
    if (event->key() == Qt::Key_Right) {
        xDelta = -step;
        m_rotationDir = DirRight;
    }
    if (event->key() == Qt::Key_Up) {
        yDelta = -step;
        m_rotationDir = DirUp;
    }
    if (event->key() == Qt::Key_Down) {
        yDelta = step;
        m_rotationDir = DirDown;
    }

    QVector2D translation = m_rendrer->xyTranslation() + QVector2D(xDelta, yDelta);
    m_rendrer->setXyTranslation(translation);

    double zAngle = 0.0;
    double yAngle = 0.0;

    double angleStep = 5;

    if (event->key() == Qt::Key_A)
        yAngle = angleStep;
    if (event->key() == Qt::Key_D)
        yAngle = -angleStep;
    if (event->key() == Qt::Key_W)
        zAngle = angleStep;
    if (event->key() == Qt::Key_S)
        zAngle = -angleStep;

    QMatrix4x4 rotation = m_rendrer->rotation();
    rotation.rotate(zAngle, QVector3D(0, 0, 1));
    rotation.rotate(yAngle, QVector3D(0, 1, 0));
    m_rendrer->setRotation(rotation);
#endif
}


void Window::rotateOneStep()
{
#if _OLD_CODE_
    double zAngle = 0.0;
    double yAngle = 0.0;

    double angleStep = 5;
    yAngle = angleStep;
    if (m_rotationDir == DirLeft)
        yAngle = angleStep;
    if (m_rotationDir == DirRight)
        yAngle = -angleStep;
    if (m_rotationDir == DirUp)
        zAngle = angleStep;
    if (m_rotationDir == DirDown)
        zAngle = -angleStep;


    QMatrix4x4 rotation = m_rendrer->rotation();
    rotation.rotate(zAngle, QVector3D(0, 0, 1));
    rotation.rotate(yAngle, QVector3D(0, 1, 0));
    m_rendrer->setRotation(rotation);
#endif
}

void Window::setCurrentGlWidget()
{
    currentGlWidget = qobject_cast<GLWidget *>(sender());
}

